package com.ciwong.epaper.modules.scan.util;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;

import com.ciwong.libs.widget.CWToast;
import com.ciwong.mobilelib.widget.CWDialog;
import com.ciwong.epaper.R;
import com.ciwong.epaper.util.IntentValue;

/**
 * 扫描工具类
 * Created by res-zwyan on 2015/7/30.
 */
public class ScanUtil
{
    /**
     *
     * 显示对话框
     *
     * @param c
     *            Activity对象类
     * @param title
     *            标题资源
     * @param msg
     *            对话框内容资源
     * @param confirm
     *            按钮资源id
     * @param call
     *            回调类
     */
    public static void alertSingleBtnDialog(final Activity c, int title,
            final String msg, int confirm, final CallBack call)
    {
        CWDialog cwDialog = new CWDialog(c, false, false);
        cwDialog.setTitle(title);
        cwDialog.setTitleTextColor(Color.BLACK);
        cwDialog.setMessage(msg, 16, Color.BLACK);

        cwDialog.setPositiveButton(c.getString(confirm),
                new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {

                        if (call != null)
                        {
                            call.callback();
                        }
                    }
                });
        cwDialog.setNegativeButton(c.getString(R.string.copy),
                new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        ScanUtil.copyContent(msg, c);
                        CWToast.makeText(c, R.string.copy_success,
                                CWToast.LENGTH_LONG).show();
                        if (call != null)
                        {
                            call.callback();
                        }
                    }
                });
        cwDialog.show();
    }

    /**
     * 兼容老接口
     *
     * @param cardType
     *            类型
     * @return 二维码类型
     */
    public static int getType(String cardType)
    {
        int type = -1;
        if (TextUtils.isEmpty(cardType))
        {
            return -1;
        }
        if ("personal".equals(cardType))
        {
            type = IntentValue.SchemaCardValue.SCHEMA_CARD_PERSONAL;
        }
        else if ("class".equals(cardType))
        {
            type = IntentValue.SchemaCardValue.SCHEMA_CARD_CLASS;
        }
        else if ("group".equals(cardType))
        {
            type = IntentValue.SchemaCardValue.SCHEMA_CARD_GROUP;

        }
        else if ("discu".equals(cardType))
        {
            type = IntentValue.SchemaCardValue.SCHEMA_CARD_DISCUSS;

        }
        else if ("publicnum".equals(cardType))
        {
            type = IntentValue.SchemaCardValue.SCHEMA_CARD_PUBLICNUM;
        }
        return type;
    }

    /**
     * 回调接口
     *
     * @author res-qqlin
     *
     */
    public static interface CallBack
    {
        /**
         *
         * 回调方法
         *
         */
        void callback();
    }

    /**
     *
     * 复制内容
     *
     * @param content
     *            内容
     * @param context
     *            上下文
     */
    public static void copyContent(String content, Context context)
    {
        int sdkVersion = Build.VERSION.SDK_INT;
        if (sdkVersion >= Build.VERSION_CODES.HONEYCOMB)
        {
            android.content.ClipboardManager copyManager = (android.content.ClipboardManager) context
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            copyManager.setText(content);
        }
        else
        {
            android.text.ClipboardManager copyManager = (android.text.ClipboardManager) context
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            copyManager.setText(content);
        }
    }
}
